package user.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import user.domain.User;


import cn.itcast.jdbc.TxQueryRunner;

public class UserDao {
	  QueryRunner qr = new TxQueryRunner();
	    /*
	     * 按用户名查询
	     */
	    public User findByUsername(String username){
	    	
	    	try {
	    		String sql =  "SELECT * FROM user WHERE username=?";
				return qr.query(sql, new BeanHandler<User>(User.class),username);
			} catch (SQLException e) {
				return null;	
				}
	    }
	    /*
	     * 插入用户
	     */
	    public void add(User user){
	    	
	    	try {
	    		String sql = "INSERT INTO user VALUES(?,?,?)";
	        	Object[] parms = {user.getUid(),user.getUsername(),user.getPassword()};
				qr.update(sql, parms);
			} catch (SQLException e) {
				throw new RuntimeException(e);	
			}
	    }
	    /**
	     * 留言方法
	     * @param username
	     * @return 
	     */
		public void message(User user) {
			try {
	    		String sql = "UPDATE user SET message=? WHERE username=?;";
	        	Object[] parms = {user.getMessage(),user.getUsername()};
		         qr.update(sql, parms);
			} catch (SQLException e) {
				throw new RuntimeException(e);	
			}
			
		}

}
